﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using dhDataAcess.baseBLL;
using dhDataAcess.orderLinqDAL;
using dhDataAcess.dataAcess.orderDataSetTableAdapters;
using dhDataAcess.dataAcess.ViewDataSetTableAdapters ;
using dhDataAcess.dataAcess;
using System.Data.SqlClient;
using dhDataAcess.orderDAL;
using dhDataAcess.sysDAL;

namespace dhSystemBLL.orderData
{
   public class orderMasterBLL
    {
       private orderMasterTableAdapter _orderMasterAdapter = null;
       private orderMasterTableAdapter orderMasterAdapter
        {
            get
            {
                if (_orderMasterAdapter == null) _orderMasterAdapter = new orderMasterTableAdapter();
                return _orderMasterAdapter;

            }
        }
       private V_orderproduceTableTableAdapter _rderproduceTableAdapter = null;
       private V_orderproduceTableTableAdapter orderproduceTableAdapter
       {
           get
           {
               if (_rderproduceTableAdapter == null) _rderproduceTableAdapter = new V_orderproduceTableTableAdapter();
               return _rderproduceTableAdapter;

           }
       }
       /// <summary>
       /// 查询视图生产通知单
       /// </summary>
       /// <returns></returns>

       public ViewDataSet.V_orderproduceTableDataTable getorderproduceTableAll()
       {
           try
           {
               return orderproduceTableAdapter.GetDataV_orderproduceTableALL();
           }
           catch (Exception ex)
           {
               throw new Exception(ex.Message.ToString());
           }
       }
       /// <summary>
       /// 根据订单状态查询视图生产通知单
       /// </summary>
       /// <returns></returns>

       public ViewDataSet.V_orderproduceTableDataTable getorderproduceTableAllByorderStatus(string orderStatus)
       {
           try
           {
               return orderproduceTableAdapter.GetDataV_orderproduceTableByorderStatus(orderStatus);
           }
           catch (Exception ex)
           {
               throw new Exception(ex.Message.ToString());
           }
       }
       /// <summary>
       /// 根据产品类型和订单号查询视图生产通知单
       /// </summary>
       /// <returns></returns>

       public ViewDataSet.V_orderproduceTableDataTable getorderproduceTableAllByptype(string orderNo,string ptype)
       {
           try
           {
               return orderproduceTableAdapter.GetDataV_orderproduceTableByptype (orderNo, ptype);
           }
           catch (Exception ex)
           {
               throw new Exception(ex.Message.ToString());
           }
       }
        /// <summary>
        /// 查询订单表
        /// </summary>
        /// <returns></returns>

       public orderDataSet.orderMasterDataTable getorderMasterAll()
        {
            try
            {
                return orderMasterAdapter.GetDataorderMasterALL();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
        /// <summary>
        /// 根据订单编号查询订单表
        /// </summary>
        /// <param name="groupNum"></param>
        /// <returns></returns>
       public orderDataSet.orderMasterDataTable getorderMasterAll(string orderNo)
       {
           try
           {
               return orderMasterAdapter.GetDataorderMasterByorderNo(orderNo);
           }
           catch (Exception ex)
           {
               throw new Exception(ex.Message.ToString());
           }
       }
       /// <summary>
       /// 根据订单编号查询订单表
       /// </summary>
       /// <param name="groupNum"></param>
       /// <returns></returns>
       public orderDataSet.orderMasterDataTable getorderMasterAllByorderNo(string orderNo)
       {
           try
           {
               return orderMasterAdapter.GetDataByorderNo (orderNo);
           }
           catch (Exception ex)
           {
               throw new Exception(ex.Message.ToString());
           }
       }
       /// <summary>
       /// 根据订单状态查询订单表
       /// </summary>
       /// <param name="groupNum"></param>
       /// <returns></returns>
       public orderDataSet.orderMasterDataTable getorderMasterAllByorderStatus(string orderStatus)
       {
           try
           {
               return orderMasterAdapter.GetDataorderMasterByorderStatus(orderStatus);
           }
           catch (Exception ex)
           {
               throw new Exception(ex.Message.ToString());
           }
       }
       /// <summary>
       /// 根据订单状态查询订单表
       /// </summary>
       /// <param name="groupNum"></param>
       /// <returns></returns>
       public orderDataSet.orderMasterDataTable getorderMasterAllByorderStatusNo(string orderStatus)
       {
           try
           {
               return orderMasterAdapter.GetDataordermasterByorderStatusNo (orderStatus);
           }
           catch (Exception ex)
           {
               throw new Exception(ex.Message.ToString());
           }
       }
       /// <summary>
       /// 插入订单表
       /// </summary>
       /// <param name="row"></param>
       /// <returns></returns>
       public int orderMasterInsert(orderMasterModel  row, orderDataSet.orderDetailDataTable  pTable, string state,string user,string orderNo,string Date)
       {
           orderDetailModel orderDetailRow = new orderDetailModel();
           int i = 0;
           SqlConnection conn = dataConn.connOpen();
           SqlTransaction tn = null;
           try
           {
               tn = conn.BeginTransaction();
               orderMasterDAL orderMaster = new orderMasterDAL(tn, conn);
               orderDetailDAL orderDetail = new orderDetailDAL(tn, conn);
               basListDAL basList = new basListDAL(tn, conn);
               if (state == "新增")
               {
                   orderMasterBLL orderMasterbll = new orderMasterBLL();
                   orderDataSet.orderMasterDataTable orderTable = orderMasterbll.getorderMasterAllByorderNo(orderNo);
                   if (orderTable.Rows.Count == 0)
                   {
                       orderMaster.orderMasterInsert(row);
                   }
                   else
                   {
                      row .orderNo  = "O" + Date + "-" + basicSql.getOrderSerail("订单序列号");
                      orderMaster.orderMasterInsert(row);
                   }
                   //更新单据编号的过程 
                  //更新订单序列号
                  string orderSerial = basicSql.getOrderSerail("订单序列号");
                  basList.basListretbasNumUpdate((Convert.ToInt32(orderSerial) + 1).ToString(), "订单序列号");

               }
               if (state == "编辑")
               {
                   orderMaster .orderMasterUpdate (row);
               }

             
               foreach (orderDataSet.orderDetailRow  detailRow in pTable)
               {
                   if (detailRow.RowState != DataRowState.Deleted)
                   {
                       orderDetailRow.orderNo =orderNo  ;  //等于主表的订单号
                       orderDetailRow.orderDetailNo = detailRow.orderDetailNo;
                       orderDetailRow.pname = detailRow.pname;
                       if (!detailRow.IshetongNoNull ())
                       {
                           orderDetailRow.hetongNo = detailRow.hetongNo;
                       }
                          
                       else { orderDetailRow.hetongNo = ""; }
                       if (!detailRow.IswaifaNull())
                       {
                           orderDetailRow.waifa = detailRow.waifa;
                       }
                       else { orderDetailRow.waifa = false; }
                       if (!detailRow.IspcolorNull ())
                       {
                           orderDetailRow.pcolor = detailRow.pcolor;
                       }
                       else { orderDetailRow.pcolor = ""; }
                       if (!detailRow.IscolorNumNull())
                       {
                           orderDetailRow.colorNum = detailRow.colorNum;
                       }
                       else { orderDetailRow.colorNum = ""; }
                       if (!detailRow.IswaifacustNull ())
                       {
                           orderDetailRow.waifacust = detailRow.waifacust;
                       }
                       else { orderDetailRow.waifacust = ""; }
                       orderDetailRow.unit = detailRow.unit;
                       orderDetailRow.qty = detailRow.qty;
                       orderDetailRow.price = detailRow.price;
                       orderDetailRow.account = detailRow.account;
                      // orderDetailRow.proType = basicSql.getproTypebyproductList(detailRow.pname);
                       if (!detailRow.IsproTypeNull())
                       {
                           orderDetailRow.proType = detailRow.proType;
                       }
                       else { orderDetailRow.proType = ""; }
                       orderDetailRow.detailStatus ="新建";
                       orderDetailRow.blStatus  = "新建";
                       orderDetailRow.ptype =detailRow.ptype  ;
                 
                       orderDetailRow.updateDate = DateTime.Now;
                           orderDetailRow.updateUser =user;
                           if (!detailRow.IsmarkNull())
                           {
                               orderDetailRow.mark = detailRow.mark;
                           }
                           else
                           {
                               orderDetailRow.mark = "";
                           }
                   
                   }

                   //编辑子表记录,新增
                   if (detailRow.RowState == DataRowState.Added)
                   {
                        
                       orderDetail.orderDetailInsert (orderDetailRow );
                       //string orderSerial = basicSql.getOrderSerail("漂染单号");
                       //basList.basListretbasNumUpdate((Convert.ToInt32(orderSerial) + 1).ToString(), "漂染单号");


                   }
                   //修改
                   if (detailRow.RowState == DataRowState.Modified)
                   {
                       orderDetail .orderDetailUpdate (orderDetailRow );
                   }
                   //删除
                   if (detailRow.RowState == DataRowState.Deleted)
                   {
                       DataRow delrow = (DataRow)detailRow;
                       orderDetail.orderDetailDel(delrow["orderDetailNo", DataRowVersion.Original].ToString());
                   }


               }

               tn.Commit();
               return i;

           }
           catch (SqlException ex)
           {
               tn.Rollback();
               if (ex.Number == 2627)
               {
                   throw new Exception("这个订单号已经存在");
               }


               throw new Exception(ex.Number.ToString() + ex.Message.ToString());

           }
           catch (Exception ex)
           {
               tn.Rollback();
               throw new Exception(ex.Message.ToString());

           }
           finally
           {
               conn.Close();
           }
       }
  /// <summary>
       /// 插入明细表信息
       /// </summary>
       /// <param name="row"></param>
       /// <returns></returns>
       public void  orderDetailInsert(orderDataSet.orderDetailDataTable pTable, string user, string orderNo, string Date)
       {
           orderDetailBLL orderDetail = new orderDetailBLL();
           orderDetailModel orderDetailRow = new orderDetailModel();
           foreach (orderDataSet.orderDetailRow detailRow in pTable)
           {
               if (detailRow.RowState != DataRowState.Deleted)
               {
                   orderDetailRow.orderNo = orderNo;  //等于主表的订单号
                   orderDetailRow.orderDetailNo = detailRow.orderDetailNo;
                   orderDetailRow.pname = detailRow.pname;
                   if (!detailRow.IspcolorNull())
                   {
                       orderDetailRow.pcolor = detailRow.pcolor;
                   }
                   else { orderDetailRow.pcolor = ""; }
                   if (!detailRow.IscolorNumNull())
                   {
                       orderDetailRow.colorNum = detailRow.colorNum;
                   }
                   else { orderDetailRow.colorNum = ""; }
                   orderDetailRow.unit = detailRow.unit;
                   orderDetailRow.qty = detailRow.qty;
                   orderDetailRow.price = detailRow.price;
                   orderDetailRow.account = detailRow.account;
                   if (!detailRow.IsproTypeNull())
                   {
                       orderDetailRow.proType = detailRow.proType;
                   }
                   else { orderDetailRow.proType = ""; }
                   orderDetailRow.detailStatus = detailRow.detailStatus ;
                   if (!detailRow.IsblStatusNull  ())
                   {
                       orderDetailRow.blStatus = detailRow.blStatus;
                   }
                   else { orderDetailRow.blStatus  = ""; }
                   orderDetailRow.detailStatus = detailRow.detailStatus;
                   orderDetailRow.ptype = detailRow.ptype;

                   orderDetailRow.updateDate = DateTime.Now;
                   orderDetailRow.updateUser = user;
                   if (!detailRow.IsmarkNull())
                   {
                       orderDetailRow.mark = detailRow.mark;
                   }
                   else
                   {
                       orderDetailRow.mark = "";
                   }
                   if (!detailRow.IshetongNoNull ())
                   {
                       orderDetailRow.hetongNo = detailRow.hetongNo;
                   }
                   else
                   {
                       orderDetailRow.hetongNo = "";
                   }
                   if (!detailRow.IswaifacustNull ())
                   {
                       orderDetailRow.waifacust = detailRow.waifacust;
                   }
                   else
                   {
                       orderDetailRow.waifacust = "";
                   }
               }

               if (detailRow.RowState == DataRowState.Modified)
               {
                   orderDetail.orderDetailUpdate(orderDetailRow);
               }

           }
       }
        /// <summary>
        /// 更新订单表
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
       public int orderMasterUpdate(orderMasterModel row)
        {
            int i = 0;
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;
            try
            {
                tn = conn.BeginTransaction();
                orderMasterDAL orderMaster = new orderMasterDAL(tn, conn);
                orderMaster.orderMasterUpdate(row);
                tn.Commit();
                return i;
            }
            catch (Exception ex)
            {
                tn.Rollback();
                throw new Exception(ex.Message.ToString());

            }
            finally
            {
                conn.Close();
            }
        }
        /// <summary>
        /// 删除一行订单表信息
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
       public int orderMasterDel(string orderNo)
        {
            int i = 0;
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;
            try
            {
                tn = conn.BeginTransaction();
                orderMasterDAL orderMaster = new orderMasterDAL(tn, conn);
                orderMaster.orderMasterDel(orderNo);
                tn.Commit();
                return i;
            }
            catch (Exception ex)
            {
                tn.Rollback();
                throw new Exception(ex.Message.ToString());

            }
            finally
            {
                conn.Close();
            }
        }
    }
}
